#include "const.h"

buffer_t b_open(const char *filename) {
	buffer_t b;
	size_t l;
	FILE *f;

	b = (buffer_t)i_new(sizeof(*b));

	i_strcpy(b->filename, sizeof(b->filename), filename);
	b->line = 1;

	b->source = (char *)0;
	b->p = (char *)0;

	b->next = (buffer_t)0;
	if ((b->prev = g_buf_tail) != (buffer_t)0)
		g_buf_tail->next = b;
	else
		g_buf_head = b;
	g_buf_tail = b;

	if (!(f = fopen(filename, "rb"))) {
		i_ioerror(filename, 0, i_errnostring());
		return (buffer_t)0;
	}

	fseek(f, 0, SEEK_END);
	l = ftell(f);
	fseek(f, 0, SEEK_SET);

	b->source = (char *)i_new(l + 1);

	fread(&b->source[0], l, 1, f);
	b->source[l] = 0;

	fclose(f);

	b->p = b->source;

	return b;
}
